package com.clone.surpassli.clone_cloudreader;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.clone.surpassli.clone_cloudreader.adapter.MyFragmentPageAdapter;
import com.clone.surpassli.clone_cloudreader.databinding.ActivityMainBinding;
import com.clone.surpassli.clone_cloudreader.ui.book.BookFragment;
import com.clone.surpassli.clone_cloudreader.ui.gank.GankFragment;
import com.clone.surpassli.clone_cloudreader.ui.menu.NavAboutActivity;
import com.clone.surpassli.clone_cloudreader.ui.menu.NavDeedBackActivity;
import com.clone.surpassli.clone_cloudreader.ui.menu.NavDownLoadActivity;
import com.clone.surpassli.clone_cloudreader.ui.menu.NavHomePageActivity;
import com.clone.surpassli.clone_cloudreader.ui.one.OneFragment;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener, ViewPager.OnPageChangeListener {

    private Toolbar mToolBar;
    private ViewPager mViewPager;
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private ActivityMainBinding mainBinding;

    private ImageView TitleGank_img;
    private ImageView TitleOne_img;
    private ImageView TitleDou_img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        initId();
        setSupportActionBar(mToolBar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            //去除默认Title显示
            actionBar.setDisplayShowTitleEnabled(false);
        }
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolBar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mDrawerLayout.setDrawerListener(toggle);
        toggle.syncState();
        mNavigationView.setNavigationItemSelectedListener(this);

//        StatusBarUtil.setColorNoTranslucentForDrawerLayout(MainActivity.this, mDrawerLayout, getResources().getColor(R.color.colorTheme));
        initContentFragment();
        initListener();
    }

    private void initId() {
        mDrawerLayout = mainBinding.drawerLayout;
        mToolBar = mainBinding.includeMain.toolBar;
        mNavigationView = mainBinding.navViewMain;
        mViewPager = mainBinding.includeMain.vpContent;
        TitleGank_img = mainBinding.includeMain.ivTitleGank;
        TitleOne_img = mainBinding.includeMain.ivTitleOne;
        TitleDou_img = mainBinding.includeMain.ivTitleDou;
    }

    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.nav_homepage) { //主页
            NavHomePageActivity.startHome(MainActivity.this);
        } else if (id == R.id.nav_download) { //下载
            NavDownLoadActivity.start(MainActivity.this);
        } else if (id == R.id.nav_feedback) { //意见反馈
            NavDeedBackActivity.start(MainActivity.this);
        } else if (id == R.id.nav_about) {    //关于我们
            NavAboutActivity.start(MainActivity.this);
        } else if (id == R.id.nav_exit) {     //退出
            finish();
        }
        return true;
    }

    private void initContentFragment() {

        ArrayList<Fragment> mFragmentArrayList = new ArrayList<>();
        mFragmentArrayList.add(new GankFragment());
        mFragmentArrayList.add(new OneFragment());
        mFragmentArrayList.add(new BookFragment());
        // 注意使用的是：getSupportFragmentManager
        MyFragmentPageAdapter adapter = new MyFragmentPageAdapter(getSupportFragmentManager(), mFragmentArrayList);
        mViewPager.setAdapter(adapter);
        // 设置ViewPager最大缓存的页面个数(cpu消耗少)
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.addOnPageChangeListener(this);
        TitleGank_img.setSelected(true);
        mViewPager.setCurrentItem(0);
    }

    private void initListener() {
        mainBinding.includeMain.ivTitleGank.setOnClickListener(this);
        mainBinding.includeMain.ivTitleDou.setOnClickListener(this);
        mainBinding.includeMain.ivTitleOne.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_gank: // 干货栏
                if (mViewPager.getCurrentItem()!=0) {
                    TitleGank_img.setSelected(true);
                    TitleOne_img.setSelected(false);
                    TitleDou_img.setSelected(false);
                    mViewPager.setCurrentItem(0);
                }
                break;
            case R.id.iv_title_one: // 电影栏
                if (mViewPager.getCurrentItem()!=1) {
                    TitleGank_img.setSelected(false);
                    TitleOne_img.setSelected(true);
                    TitleDou_img.setSelected(false);
                    mViewPager.setCurrentItem(1);
                }
                break;
            case R.id.iv_title_dou:  // 书籍栏
                if (mViewPager.getCurrentItem()!=2) {
                    TitleGank_img.setSelected(false);
                    TitleOne_img.setSelected(false);
                    TitleDou_img.setSelected(true);
                    mViewPager.setCurrentItem(2);
                }
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                TitleGank_img.setSelected(true);
                TitleOne_img.setSelected(false);
                TitleDou_img.setSelected(false);
                break;
            case 1:
                TitleGank_img.setSelected(false);
                TitleOne_img.setSelected(true);
                TitleDou_img.setSelected(false);
                break;
            case 2:
                TitleGank_img.setSelected(false);
                TitleOne_img.setSelected(false);
                TitleDou_img.setSelected(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}